/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * MeetPeroidTaskController
 * 会议周期任务
 * 修改纪录
 * 2020-11-04 版本：1.0 donghang 创建。
 * 
 * @version 版本：1.0
 * @author 作者：donghang
 * 创建日期：2020-11-04
 */
package com.topscomm.meet.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.meet.controller.auto.MeetPeroidTaskControllerAuto;
import com.topscomm.meet.pojo.MeetPeroidTaskEntity;
import com.topscomm.meet.pojo.MeetTypeManageEntity;
import com.topscomm.meet.service.IMeetTypeManageService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: donghang
 * @date: 2020-11-04
 * @description:会议周期任务Controller
 * @modify:
 */
@RestController
@RequestMapping("meet/peroidTask")
public class MeetPeroidTaskController extends MeetPeroidTaskControllerAuto implements IMeetPeroidTaskController {
	@Autowired
	private IMeetTypeManageService meetTypeManageService;

	/**
	 * @author: donghang
	 * @date: 2020-11-04
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (name like '%{0}%'  or keywords like '%{1}%')", value, value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_meettypeid")) {
			String value = paraMap.get("qry_meettypeid").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and meettypeid = '{0}'", value);
			}
		}
		if (paraMap.containsKey("qry_periodtype")) {
			String value = paraMap.get("qry_periodtype").toString();
			if (!StringUtil.isEmpty(value)) {
				sql.appendSingle(" and periodtype = '{0}'", value);
			}
		}
		// 查看自己或管理用户的
		String[] manageParams = { "','", "manageusercodes", "','" };
		sql.appendSingle(" and (usercode = '{0}' or  ({1}) like '%{2}%') ", this.getUserBean().getUser().getCode(),
				SQLAdapter.concat(manageParams), "," + this.getUserBean().getUser().getCode() + ",");
		return sql.toString();
	}

	/**
	 * @author: donghang
	 * @date: 2020-11-04
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @description:
	 * @param dataMap
	 * @author: donghang
	 * @date: 2020年11月9日下午8:19:40
	 * @modify:
	 */
	@Override
	protected void afterLoad(Map<String, Object> dataMap) {
		// 查询会议类型上级节点
		String meetTypeId = ConvertUtil.convertToString(dataMap.get("meettypeid"));
		List<String> meetTypeList = new ArrayList<>();
		if (meetTypeId.length() > 0) {
			List<MeetTypeManageEntity> parentList = this.meetTypeManageService.queryByWhere("id=" + meetTypeId);
			if (parentList.size() == 1) {
				if (parentList.get(0).getParentid() > 0)
					meetTypeList.add(ConvertUtil.convertToString(parentList.get(0).getParentid()));
			}
			meetTypeList.add(meetTypeId);
		}
		dataMap.put("selectmeettypelist", meetTypeList);
	}

	/**
	 * 保存数据
	 * 
	 * @description:
	 * @param paraMap
	 * @return
	 * @author: donghang
	 * @date: 2020年11月3日下午12:49:47
	 * @modify:
	 */
	@RequestMapping(value = "/save", method = { RequestMethod.POST, RequestMethod.PUT })
	public ResponseResult<Object> save(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			MeetPeroidTaskEntity meetPeroidTaskEntity = this.service.save(paraMap, this.getUserBean());
			return ResponseResult.ok(meetPeroidTaskEntity);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * 会议发布
	 * 
	 * @description:
	 * @param paraMap
	 * @return
	 * @author: donghang
	 * @date: 2020年11月3日上午8:48:36
	 * @modify:
	 */
	@PostMapping(value = "/publish")
	public ResponseResult<Object> publish(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			this.service.publish(paraMap, this.getUserBean());
			return ResponseResult.ok("发布成功");
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @description: 会议作废
	 * @param paraMap
	 * @return
	 * @author: donghang
	 * @date: 2020年9月1日下午4:22:58
	 * @modify:
	 */
	@PostMapping(value = "/cancel")
	public ResponseResult<Object> cancel(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			this.service.cancel(paraMap, this.getUserBean());
			return ResponseResult.ok("周期会议已终止");
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * 移交权限
	 * 
	 * @description:
	 * @param paraMap
	 * @return
	 * @author: donghang
	 * @date: 2020年11月23日上午11:21:41
	 * @modify:
	 */
	@PostMapping(value = "/transfer")
	public ResponseResult<Object> transfer(@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			this.service.transfer(paraMap);
			return ResponseResult.ok("移交成功");
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
}
